//app.js
App({
  onLaunch: function () {
    // 展示本地存储能力
    var logs = wx.getStorageSync('logs') || []
    logs.unshift(Date.now())
    wx.setStorageSync('logs', logs)
    //判断机型(适配iphoneX)
    wx.getSystemInfo({
      success: (res) => {
        this.globalData.systemInfo = res;
        if (res.model.search('iPhone X') != -1) {
          this.globalData.isIphoneX = true
        }
      }
    })
    wx.getSystemInfo({
      success: res => {
        //导航高度
        this.globalData.navHeight = res.statusBarHeight + 46;
      }, fail(err) {
        console.log(err);
      }
    })
    //系统更新
    if (wx.canIUse('getUpdateManager')) {
      const updateManager = wx.getUpdateManager();
      updateManager.onCheckForUpdate(function (res) {
        if (res.hasUpdate) {
          updateManager.onUpdateReady(function () {
            wx.showModal({
              title: '更新提示',
              content: '新版本已经准备好，是否重启应用？',
              success: function (res) {
                if (res.confirm) {// 新的版本已经下载好，调用 applyUpdate 应用新版本并重启
                  updateManager.applyUpdate()
                }
              }
            })
          });
          updateManager.onUpdateFailed(function () {
            console.log('已经是最新版本');
          })
        }
      })
    } else {
      wx.showModal({
        title: '提示',
        content: '当前微信版本过低，无法使用该功能，请升级到最新微信版本后重试',
      })
    }
  },
  checklogin(userInfo,flag = ''){
    wx.getSetting({
      success: res => {
        if (res.authSetting['scope.userInfo']) {
          if(userInfo){
            this.userlogin(userInfo,flag);
          }else{
            wx.switchTab({
              url: '../my/my',
            })
          }
        }else{
          wx.showModal({
            title: '登录',
            content: '请授权登录，享受更多优惠',
            confirmText:'去授权',
            success:function(res){
              if(res.confirm){
                wx.switchTab({
                  url: '../my/my',
                })
              }
            }
          })
        }
      },
      fail: function () {
        wx.switchTab({
          url: '../my/my',
        })
      }
    });
  },
  userlogin(userInfo = '',flag) {
    let that = this;
    wx.checkSession({
      success: function () {
        wx.getStorage({
          key: 'loginSessionKey',
          success: function(res) {
            if(flag==1){
              wx.navigateBack({
                delta:1
              })
            }
          },
          fail:function(res){
            that.onlogin(userInfo,flag);
          }
        })
      },
      fail: function () {
        that.onlogin(userInfo,flag);
      }
    })
  },
  /*
  * 登录获取code
  */
  onlogin(userInfo = '',flag) {
    var that = this;
    wx.login({
      success: function (res) {
        if (res.code) {
          wx.request({
            url: that.globalData.requestUrl+'getWxLoginCode',
            method: 'POST',
            data: {
              'code': res.code,
              'nickName': userInfo.nickName,
              'avatarUrl': userInfo.avatarUrl,
              'gender': userInfo.gender,
              'city': userInfo.city,
              'province': userInfo.province,
              'country': userInfo.country
            },
            header: {
              'content-type': 'application/x-www-form-urlencoded',
              'Accept': 'application/json'
            },
            success: res => {
              if (res.data['code'] == 1) {
                that.globalData.userInfo = userInfo;
                wx.setStorageSync('loginSessionKey', res.data['errorMsg']);
                wx.setStorageSync('user_id', res.data['id']);
                if (flag == 1) {
                  wx.navigateBack({
                    delta: 1
                  })
                }
              } else {
                console.log('code get api fail:' + res.data['errorMsg']);
              }
            }
          })
        }
      },
      fail: function () {
      }
    })
  },
  globalData: {
    systemInfo: null,
    userInfo: null,
    isIphoneX: false,
    requestUrl:'',
    sharetitle:'魔术学习小程序',//分享标题
    shareimage:'',//分享图片
    sharepath:'pages/index/index',//分享路径
    imgUrl:'',//后台图片地址
    navHeight: 0,
    navTop: 0,
    jnheight: 0,
    jnwidth: 0,  
    selected:0,
    shopid:null,
  },
  //自定义导航
  onLaunch: function (option) {
    let menuButtonObject = wx.getMenuButtonBoundingClientRect();
    wx.getSystemInfo({
      success: res => {
        let statusBarHeight = res.statusBarHeight,
          navTop = menuButtonObject.top,//胶囊按钮与顶部的距离
          navHeight = statusBarHeight + menuButtonObject.height + (menuButtonObject.top - statusBarHeight)*2;//导航高度
        this.globalData.navHeight = navHeight;
        this.globalData.navTop = navTop;
        this.globalData.windowHeight = res.windowHeight;
        this.globalData.windowWidth = res.windowWidth;
        this.globalData.jnheight = menuButtonObject.height;//胶囊的高度
        this.globalData.jnwidth = menuButtonObject.width;//胶囊的宽度
      },
      fail(err) {
        console.log(err);
      }
    })
  },
})